<?php
defined('BASE') or exit('Access Denied!');

/**
 * Obullo Framework (c) 2009.
 *
 * PHP5 MVC-min Framework software for PHP 5.2.4 or newer
 * Derived from Code Igniter 
 * 
 * @package         obullo       
 * @author          obullo.com
 * @copyright       Ersin Güvenç (c) 2009.
 * @filesource
 * @license
 */
 
/**
 * Obullo Content Builder Class
 *
 * @package     Obullo
 * @subpackage  Libraries
 * @category    Language
 * @author      Ersin Güvenç
 * @link        
 */
class OB_Content
{

    /**
    * Constructor
    *
    * @access    public
    */
    public function __construct()
    {    
        log_message('debug', "Content Class Initialized");
    }
    
    // --------------------------------------------------------------------
    
    public function js($filename, $arguments = '')
    {   
        return $this->_js($filename, $arguments, 'local'); 
    }
    
    // --------------------------------------------------------------------
    
    public function source_js($filename, $arguments = '')
    {   
        return $this->_js($filename, $arguments, 'source');
    }
    
    // ------------------------------------------------------------------------
    
    public function script($filename, $data = array())
    {   
        return $this->_script(DIR .$GLOBALS['d']. DS .'scripts'. DS, $filename, $data);
    }
    
    // ------------------------------------------------------------------------
    
    public function app_script($filename, $data = array())
    {   
        return $this->_script(APP .'scripts'. DS, $filename, $data);
    }
    
    // ------------------------------------------------------------------------
    
    public function base_script($filename, $data = array())
    {   
        return $this->_script(BASE .'scripts'. DS, $filename, $data);
    }
    
    // ------------------------------------------------------------------------
    
    public function css($filename, $arguments = '', $path = 'css')
    {   
        return $this->_css($filename, $arguments, $dir = 'local', $path);
    }

    // ------------------------------------------------------------------------
    
    public function source_css($filename, $arguments = '', $path = 'css')
    {   
        return $this->_css($filename, $arguments, $dir = 'source', $path);
    }
    
    // ------------------------------------------------------------------------
    
    public function view($filename, $data = array(), $string = TRUE)
    {               
        return $this->_view(DIR .$GLOBALS['d']. DS .'views'. DS, $filename, $data, $string);
    }
    
    // ------------------------------------------------------------------------
    
    public function app_view($filename, $data = array(), $string = FALSE)
    {
        return $this->_view(APP .'views'. DS, $filename, $data, $string); 
    }
   
    /**
    * Build js files in <head> tags
    * 
    * @author   Ersin Güvenç
    * @param    string $filename
    * @param    string $arguments
    * @param    string $dir
    */
    private function _js($filename, $arguments = '', $dir = 'source')
    {
        switch ($dir)
        {
           case 'local':
           $url = config_item('base_url').'application/directories/'.$GLOBALS['d'].'/views/js/';
             break;
             
           case 'source':
           $url = config_item('source_url').'js/';
             break;
        }
    
       return "\n".'<script type="text/javascript" src="'.$url.$filename.'.js'.$arguments.'"></script>'; 
    }
    
    /**
    * Build css files in <head> tags
    * 
    * @author   Ersin Güvenç
    * @param    mixed   $filename array or string
    * @param    string  $media  'all' or 'print' etc..
    * @param    string  $arguments add argument
    * @param    string  $dir application or directory css
    * @version  0.1
    * @version  0.2 added $path variable
    * @return   string
    */
    private function _css($filename, $arguments = ' media="all" ', $dir = 'source', $path = 'css')
    {
        if( ! is_array($filename))
        $filename = array($filename);
        
        if(empty($arguments))
        {
            $arguments = ' media="all" ';
        }
        
        $style = "<style type=\"text/css\" $arguments>\n";
    
        switch ($dir)
        {
           case 'local':
           $url = config_item('base_url').'application/directories/'.$GLOBALS['d'].'/views/'.$path.'/';
             break;
             
           case 'source':
           $url = config_item('source_url').$path.'/';
             break;
        }
        
        foreach($filename as $key => $css)
        {
            $style.= "@import url(\"".$url.$css.'.css'."\");\n";
        }
        
        $style.= '</style>';
        
        return $style;   
    }
    
    // --------------------------------------------------------------------
    
    /**
    * Load Java script files externally
    * like fetch view files as string
    * 
    * @author   Ersin Güvenç
    * @param    string $path
    * @param    string $filename
    * @param    array  $data
    */
    private function _script($path, $filename, $data = array())
    {
        if ( ! file_exists($path . $filename . EXT) )
        {
            throw new LoaderException('Unable locate the script file: '. $filename . EXT);
        } 
        
        if(sizeof($data) > 0) { extract($data, EXTR_SKIP); }
        
        ob_start();
        
        include($path . $filename . EXT);
        $content = ob_get_contents();
        
        ob_end_clean();

        return "\n".$content; 
    }
    
    // --------------------------------------------------------------------
    
    /**
    * Main view function
    * 
    * @author   Ersin Güvenç          
    * @param    string $path file path 
    * @param    string $filename 
    * @param    array $data template vars
    * @param    boolen $string 
    * @version  0.1
    * @return   void
    */
    private function _view($path, $filename, $data = array(), $string = FALSE)
    {
        if (file_exists($path . $filename . EXT))
        {
            if($string)
            {
                if(sizeof($data) > 0) { extract($data, EXTR_SKIP); }
        
                ob_start();
                
                include($path . $filename . EXT);
                $content = ob_get_contents();
                
                ob_end_clean();
                
                return $content;
            } 
            
            // just include file.
            if(sizeof($data) > 0) { extract($data, EXTR_SKIP); }
            
            include($path . $filename . EXT);
            return;
        } 
        
        throw new LoaderException('Unable to locate the view: ' . $filename . EXT);
    }
    
    
}
// END Content Class

/* End of file Content.php */
/* Location: ./base/libraries/Content.php */

?>